package com.vallis.asfc.controller;


import cn.hutool.core.util.IdcardUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vallis.asfc.entity.Organization;
import com.vallis.asfc.entity.User;
import com.vallis.asfc.mapper.UserMapper;
import com.vallis.asfc.service.OrganizationService;
import com.vallis.asfc.service.UserService;
import com.vallis.asfc.util.NativePlace;
import com.vallis.asfc.util.Pinyin4jUtil;
import com.vallis.asfc.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author vallis
 * @since 2020-06-14
 */
@RestController
@RequestMapping("/user")
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private OrganizationService organizationService;

    /**
     * 查询全部
     * @return
     */
    @GetMapping("getAllUser")
    public Result getAllUser(){
        System.out.println("调用成功");
        return new Result(200,"调用成功",userService.list());
    }

    /**
     * 分页查询
     * @param query
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("getUsers")
    public Result getUsers(String query,Integer pageNum,Integer pageSize){
        System.out.println("调用分页查询");

        if(null!=query&&query.length()>5){
            System.out.println("搜索会员单位");
            QueryWrapper<User> wrapperx = new QueryWrapper();
            QueryWrapper<User> deptname = wrapperx.like("user_dept_name", query);
            Page<User> pagec=new Page<User>(pageNum,pageSize);

            Page<User> pagecx = userService.page(pagec, deptname);

            return  new Result(200,"调用成功",pagecx);
        }
        QueryWrapper<User> wrapper = new QueryWrapper();
        if (null!=query){
            System.out.println("获取的名字是："+query);
            QueryWrapper<User> userName = wrapper.like("user_name", query);
            Page<User> page=new Page<User>(pageNum,pageSize);

            Page<User> page1 = userService.page(page, userName);

            return  new Result(200,"调用成功",page1);
        }else {
            wrapper.gt("user_id",0);
            Page<User> page=new Page<User>(pageNum,pageSize);

            Page<User> page1 = userService.page(page,wrapper);

            return  new Result(200,"调用成功",page1);

        }

    }



    /**
     * 根据id查询
     * @param userId
     * @return
     */
    @GetMapping("getById")
    public Result getById(Long userId){
        System.out.println("获取单个用户的id:"+userId);
        return  new Result(userService.getById(userId));
    }

    /**
     * 保存用户
     * @param userName
     * @param userCertnumber
     * @return
     */
    @PutMapping("saveUser")
    public Result saveUser(String userName,String userCertnumber,Long deptId){

        User u=new User();

            u.setUserName(userName);
            u.setUserPassword("asfchn.cn");
            //获取会员单位id
            u.setUserDeptId(deptId);
        Organization byId = organizationService.getById(deptId);
        if (null!=byId){
            u.setUserDeptName(byId.getOrgName());
        }else {
            u.setUserDeptName("待输入");
        }


        u.setUserGender(IdcardUtil.getGenderByIdCard(userCertnumber));
        u.setUserCertnumber(userCertnumber);
        u.setUserAge(IdcardUtil.getAgeByIdCard(userCertnumber) );
        int nativePlaceCode=Integer.parseInt(userCertnumber.substring(0, 6));
        String nativePlace = NativePlace.getNativePlace(nativePlaceCode);
        u.setUserCity(nativePlace);
        u.setUserState(0);
        String city =NativePlace.getNativeCity(nativePlaceCode);
        String pinYinHeadChar = Pinyin4jUtil.getPinYinHeadChar(city);

         userService.save(u);
        u.setUserSnNumber(pinYinHeadChar+(u.getUserId()+18092));
        return new Result("保存成功", userService.save(u));
    }

    /**
     * 删除用户
     * @param userId
     * @return
     */
    @PutMapping("deleteUser")
    public Result deleteUser(Long userId){
        System.out.println("删除的id为"+userId);
        return new Result("删除成功",userService.removeById(userId));
    }

    /**
     * 更新用户
     * @param user
     * @return
     */
    @PostMapping("updateUser")
    public Result updateUser(User user){

        System.out.println(user);

        return new Result("更新成功",userService.updateById(user));
    }

    /**
     *更改状态
     * @param userId
     * @param state
     * @return
     */
    @PutMapping("updateUserState")
    public Result updateUserState(Long userId,int state){
        User u= userService.getById(userId);
         u.setUserState(state);
    return new Result("更新成功",userService.updateById(u));
    }

    /**
     * 重置密码
     * @param userId
     * @return
     */
    @PutMapping("restPassWord")
    public Result restPassWord(Long userId){
            User u= userService.getById(userId);
            u.setUserPassword("asfchn.cn");
        System.out.println("开始重置"+u.getUserName()+"的密码");
            return new Result("更新成功",userService.updateById(u));
    }

    /**
     * 获取会员数量
     * @return
     */
    @GetMapping("getUserNum")
    public Result getUserNum(){
        System.out.println("开始获取用户数量");
        Map<String, Integer> r = new HashMap();
    
        r.put("userNum", userService.count());
        r.put("userpayed", userService.count(new QueryWrapper<User>().eq("user_state", 1)));
        r.put("usernotpayed", userService.count(new QueryWrapper<User>().eq("user_state", 0)));
        r.put("lienseNUm", userService.count(new QueryWrapper<User>().ne("user_fly_license_id", null)));
        r.put("orgcount", organizationService.count());
        r.put("payorgcount", organizationService.count(new QueryWrapper<Organization>().eq("org_state", 1)));
    
        return new Result(200, "调用成功", r);
    }

    /**
     * 获取正式会员的数量
     * @return
     */
    @GetMapping("getFormalUserNum")
    public Result getFormalUserNum(){
        return new Result("会员数量", userService.count(new QueryWrapper<User>().eq("user_state", 1)));
    }
    
    /**
     * 获取非正式会员的数量
     *
     * @return
     */
    @GetMapping("getUnFormalUserNum")
    public Result getUnFormalUserNum() {
        return new Result("会员数量", userService.count(new QueryWrapper<User>().eq("user_state", 0)));
    }
    
    @GetMapping("getThisOrgUser")
    public Result getThisOrgUser(Long userDeptId) {
        System.out.println("查询该会员单位所有会员");
        List<User> usersByDept = userService.list(new QueryWrapper<User>().eq("user_dept_id", userDeptId));
        
        
        return new Result(200, "获取该会员单位的所有会员", usersByDept);
        
    }
    
    /**
     * 获取非正式会员的数量
     *
     * @return
     */
    @GetMapping("showUserInWelcome")
    public Result showUserInWelcome() {
        System.out.println("欢迎页获取已交费会员");
        List<User> user_state = userService.list(new QueryWrapper<User>().eq("user_state", 1));
        System.out.println(user_state.size());
        return new Result("会员数量", user_state);
    }
    
}

